package com.yixun.lbx.service.impl;

import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.entity.YxProCompanyStaff;
import com.yixun.lbx.mapper.company.CompanyMapper;
import com.yixun.lbx.mapper.company.CompanyStaffMapper;
import com.yixun.lbx.service.CompanyStaffService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional
public class CompanyStaffServiceImpl implements CompanyStaffService {
    private static final String FILEPATH = "C:/wisdom_bee/upload/";
    private final CompanyStaffMapper companyStaffMapper;
    private final CompanyMapper companyMapper;
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
    public CompanyStaffServiceImpl(CompanyStaffMapper companyStaffMapper, CompanyMapper companyMapper) {
        this.companyStaffMapper = companyStaffMapper;
        this.companyMapper = companyMapper;
    }
    
    @Override
    public Integer addCompanyStaff(HashMap<String, Object> map) {
        map.put("addTime", dateFormat.format(new Date()));
        int i = companyStaffMapper.insetCompanyStaff(map);
        return i;
    }
    
    @Override
    public Integer updateCompanyStaff(HashMap<String, Object> map) {
        map.put("updateTime", dateFormat.format(new Date()));
        int i = companyStaffMapper.updateCompanyStaff(map);
        return i;
    }
    
    @Override
    public Integer deleteCompanyStaff(HashMap<String, Object> map) {
        map.put("updateTime", dateFormat.format(new Date()));
        int i = companyStaffMapper.deleteCompanyStaff(map);
        return i;
    }
    
    @Override
    public List<YxProCompanyStaff> findCompanyStaffByAngs(YxProCompanyStaff companyStaff) {
        return companyStaffMapper.findCompanyStaffByAngs(companyStaff);
    }
    
    @Override
    public Integer csCount(HashMap<String, String> map) {
        return companyStaffMapper.getCsCount(map);
    }
    
    @Override
    public List<YxProCompanyStaff> getListByPage(HashMap<String, String> map) {
        YxProCompany company = new YxProCompany();
        company.setCode(map.get("code"));
        YxProCompany proCompany = companyMapper.getCompanyByIdOrNameOrCode(company);
        if (proCompany != null) {
            Long qyId = proCompany.getQyId();
            map.put("qyId", String.valueOf(qyId));
        }
        return companyStaffMapper.getList(map);
    }
    
    @Override
    public List<YxProCompanyStaff> getSearchListByPage(HashMap<String, String> map) {
        return companyStaffMapper.getSearchListByPage(map);
        
    }
    
    @Override
    public YxProCompanyStaff getOneById(String id) {
        return companyStaffMapper.getOneById(id);
    }
    
    /**
     * @param databaseFilePath 数据库 相关文件的地址
     * @apiNote deleteFile 删除上传的文件
     */
    public void deleteFile(String databaseFilePath) {
        
        String sysFilepath = FILEPATH;
        String substringPath = databaseFilePath.split("/file/")[1];
        String realPath = sysFilepath + substringPath;
        File file = new File(realPath);
        if (file.exists()) {
            new File(realPath).delete();
        }
    }
}
